<?php

/**
 * 
 * @author yuxun
 *
 */
class ServerStateService extends Service {
	
	public static function monitor($game_id,$time){
		if($game_id == ''){
			$json_arr = array('state'=>0,'msg'=>'need to parameter game_id' );
			return $json_arr;
		}
		
		if($game_id != 30){
			$json_arr = array('state'=>0,'msg'=>'Parameter value error game_id' );
			return $json_arr;
		}
		
		if($time == ''){
			$json_arr = array('state'=>0,'msg'=>'need to parameter time' );
			return $json_arr;
		}
		
		$modMonitordb = new ModMonitordb();
		$serverInfoList = $modMonitordb->getServerInfoListBy();
// 				echo '<pre>';
// 				var_dump($serverInfoList);
		
		if(count($serverInfoList) == 0){
			$json_arr = array('state'=>0,'msg'=>'no data' ); //  没有数据
			return $json_arr;
		}
		
		$d_arr_arr = array();
		foreach ($serverInfoList as $key=>$val){
			$run_bash = $val['run_bash'];
			
			$run_bash_list = $modMonitordb->online($run_bash);
			
			$d_arr = array();
			$d_arr['ip'] = $val['ip'];
			$d_arr['load'] = $val['load'];
			$d_arr['space_all'] = $val['space_all'];
			$d_arr['space_use'] = $val['space_use'];
			$d_arr['mem_all'] = $val['mem_all'];
			$d_arr['mem_free'] = $val['mem_free'];
			$d_arr['server_num'] = $val['server_num'];
			$d_arr['online_num'] = 0;
			$d_arr['online_top_num'] = 0;
			$d_arr['online_detail'] = array();
			
			$online_num = 0;
			$online_top_num = 0;
			$r_data = array();
			foreach ($run_bash_list as $key=>$val){
				$d = array();
				$d['plat_id'] = $val['plat_id'];
				$d['server_id'] = $val['server_id'];
				$d['s_num'] = $val['s_num'];
				$d['top_num'] = $val['top_num'];
				array_push($r_data, $d);
				$online_num += $val['s_num'];
				$online_top_num += $val['top_num'];
			}
			$d_arr['online_num'] = $online_num;
			$d_arr['online_top_num'] = $online_top_num;
			$d_arr['online_detail'] = $r_data;
			
			array_push($d_arr_arr, $d_arr);
		}
		
		$result['state'] = true;
		if($result['state']){
			//$json_arr = array('state'=>1,'msg'=>'success','data'=>$data);
			$json_arr = $d_arr_arr;
			return $json_arr;
		}else{
			$json_arr = array('state'=>0,'msg'=>'fail' );
			return $json_arr;
		}
		
	}
	
}